/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <string>
#include <vector>
#include <memory>
#include <baja/db/db.h>
#include <dependencies/includes/ink_renderer_interface.h>
#include <journal/models/db/table_interfaces.h>

namespace journal { namespace model { namespace db
{

namespace bi = baja::ink;

class ink_stroke_table : public iink_stroke_table,
                         public icommon_table_client
{
public:
    ink_stroke_table();
    static std::shared_ptr<iink_stroke_table> create(std::shared_ptr<s::iengine> dbEngine);

    // icommon_table_client
    void defineColumns(std::shared_ptr<s::itable> table);
    void defineIndexColumns(std::shared_ptr<s::iindex> primaryKeyIndex);
    void defineRowCells(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> row);

    // ibase_table
    std::shared_ptr<icommon_table> common();

    // iink_stroke_table
    void readIds(b::uint32 inkSurfaceId,
                 std::vector<b::uint32>& ids);

    void insert(b::uint32 inkStrokeId, 
                b::uint32 inkSurfaceId,
                b::uint64 t0,
                b::uint32 color,
                b::float32 width,
                s::binary_blob& pointsArray);

    void update(b::uint32 inkStrokeId, 
                b::uint32 inkSurfaceId,
                b::uint64 t0,
                b::uint32 color,
                b::float32 width,
                s::binary_blob& pointsArray);

    void load(b::uint32 inkStrokeId, 
              b::uint32 inkSurfaceId,
              b::uint64& t0,
              b::uint32& color,
              b::float32& width,
              std::vector<bi::ink_point>& pointsArray);

private:
    std::recursive_mutex _mutex;

    std::shared_ptr<icommon_table> _commonTable;
    std::weak_ptr<icommon_table_client> _commonTableClient;

    std::shared_ptr<s::icolumn_typed<b::uint32>> _inkStrokeIdColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _inkSurfaceIdColumn;
    std::shared_ptr<s::icolumn_typed<b::uint64>> _inkT0Column;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _inkColorColumn;
    std::shared_ptr<s::icolumn_typed<b::float32>> _inkWidthColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _inkPointsSizeColumn;
    std::shared_ptr<s::icolumn_typed<s::binary_blob>> _inkPointsColumn;

    std::shared_ptr<s::icell_typed<b::uint32>> _inkStrokeIdCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _inkSurfaceIdCell;
    std::shared_ptr<s::icell_typed<b::uint64>> _inkT0IdCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _inkColorCell;
    std::shared_ptr<s::icell_typed<b::float32>> _inkWidthCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _inkPointsSizeCell;
    std::shared_ptr<s::icell_typed<s::binary_blob>> _inkPointsCell;

    std::shared_ptr<s::iindex_column> _inkSurfaceIndexColumn;
    std::shared_ptr<s::iindex_column> _inkStrokeIndexColumn;

    void read(b::uint32 inkStrokeId,
              b::uint32 inkSurfaceId,
              std::shared_ptr<s::iresult> result);
};

std::shared_ptr<iink_stroke_table> createInkStrokeTable(std::shared_ptr<s::iengine> dbEngine);

}}} // namespace journal::model::db